package com.ease.util {

   /**
    * A queue based function caller that queues the provided functions until an invoke
    * is explicitly requested.
    */

   public class QueuingFunctionCaller {
      private var _queue:Array/*FunctionInfo*/ = [];

      /**
       * Adds the provided function to the queue of functions awaiting invocation.
       *
       * @param funct
       *    The function to call.
       *
       * @param params
       *    The function parameters or null if none.
       */
      public function enqueue(funct:Function, params:Array):void {
         _queue.push(new FunctionInfo(funct, params));
      }

      /**
       * Flushes the queued functions.
       */
      public function callPendingFunctions():void {
         if (_queue.length == 0) {
            return;
         }
         var infos:Array/*FunctionInfo*/ = _queue;
         _queue = [];
         for each (var info:FunctionInfo in infos) {
            info.funct.apply(null, info.params);
         }
      }
   }
}

class FunctionInfo {
   public function FunctionInfo(funct:Function, params:Array) {
      this.funct = funct;
      this.params = params;
   }
   public var funct:Function;
   public var params:Array;
}